import QtQuick 1.1
import com.nokia.meego 1.0

import "../Script.js" as Script_list
import "../Calcul.js" as Script
import "../tools"
Page {
    id: page
    state: page.state = "showBrowser"
    onVisibleChanged: {
        Script_list.func(list,0,0,0);

    }
    function loading(){
        busyindicator.opacity = 1;
        busyindicator.running = true;
    }
    function finish(){
        busyindicator.opacity = 0;
        busyindicator.running = false;
    }
    function speed(progression, max){
//        Script.time_actuel = Qt.formatDateTime(new Date(), "hhmmss");

//        console.debug("Lancement a  = " + Script.time_debut  + " ---- ACTUELLE --- = " + Script.time_actuel + " estimation = " +
//                      (progression - max)*(progression) / (Script.time_debut - Script.time_actuel) );

//        console.debug("Speed = " + (progression)/(Script.time_actuel - Script.time_debut)  + " kb/s")
    }

    property int selectedItem: -1
    property string title : Script.getArray()
    property string image_source: "icon_img"
    tools:ToolBarLayout {
        id: commonTools
        ToolIcon {
            platformIconId: "toolbar-tab-previous"
            onClicked: {
//                ftpManager.deconnecter();
                if(Script.checkArrayNb()){
                    ftpManager.Cd("..")
                    commonTools.enabled = true
                    Script.delArray();
                }
                else{
                    commonTools.enabled = false
                }
            }
        }
        ToolIcon {
            platformIconId: "toolbar-delete"
            onClicked: {
                rectangle_content.state = "delete"
            }
        }
        ToolIcon {
            platformIconId: "toolbar-add"
            onClicked: {
//                dialog.open()
//                fileManager.Cd("c:/")
                fileManager.Cd("/home/user/MyDocs/")
//                page.state = "showBrowser"
                localBrowser.height = 300;

            }
        }
    }
    MyDialog{
        id: dialog
        onCreate: {
            console.debug(name)
            ftpManager.createFolder(name);
        }

    }
    LocalBrowser{
        id: localBrowser
        y: 340
        height: 0
        visible: false
        anchors.bottom: parent.bottom
        anchors.bottomMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
        onUploadClicked: console.debug(file)
        Behavior on height { NumberAnimation { easing.type: Easing.OutQuad; duration: 400 }}
    }

    Connections{
        target: ftpManager
        ignoreUnknownSignals: true
        onDownloadProgress:{
            progressBar.opacity = 1
            progressBar.value = progress;
            progressBar.maximumValue = total;
            progressBar.minimumValue = 0;
            speed(progress, total);
        }
        onDownloadSuccesfull: progressBar.opacity = 0
        onListingFinished: finish();
    }
    Rectangle { //Header
        id: pageHeader
        anchors {
            top: page.top
            left: page.left
            right: page.right
        }

//        height: parent.width < parent.height ? 72 : 20
        height: 40
        width: parent.width
        color: "transparent"
//        source: "image://theme/meegotouch-view-header-fixed" + (theme.inverted ? "-inverted" : "")
        z: 1

        Label {
            id: header
            anchors {
                verticalCenter: parent.verticalCenter
                left: parent.left
                leftMargin: 16
            }
            platformStyle: LabelStyle {
                fontFamily: "Nokia Pure Text Light"
                fontPixelSize: 30
            }
            text: page.title
//            font.pixelSize: 10
        }
        BusyIndicator {
            id: busyindicator
            x: 294
            y: 15
            width: 50
            height: 50
            anchors.top: parent.top
            opacity: 0;
            anchors.topMargin: 15
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 15
            anchors.right: parent.right
            anchors.rightMargin: 21
            Behavior on opacity { NumberAnimation { duration: 600 }}
        }
    }

    ListView {
        id: listview
        width: 153
        anchors.top: progressBar.bottom
        anchors.topMargin: 0
        anchors.rightMargin: 0
        anchors.bottomMargin: 0
        anchors.right: parent.right
        anchors.bottom: localBrowser.top
        anchors.left: parent.left
        clip: true
        focus: true
        delegate: delegate
        highlight: Rectangle { color: "lightsteelblue"; radius: 5 }
        highlightFollowsCurrentItem: true

    model: ListModel {
            id: list
        }
    }
    ScrollDecorator{
        id: scrolldetector
        flickableItem: listview
    }

    Component { //Delegate
        id: delegate
        Rectangle{
            id: rectangle_content
            width: 500
            height: 100
            anchors.right: parent.right
            anchors.left: parent.left
            color: "transparent"
//            color: listview.isCurrentItem ? "lightsteelblue" : "transparent"
            MouseArea{
                id:main_mouse
                anchors.leftMargin: 0
                anchors.rightMargin: 0
                anchors.top: parent.top
                anchors.bottom: parent.bottom
                anchors.left: parent.left
                anchors.right: button_go.left
                onClicked: {
                    if(listview.currentIndex === number.text){
                        rectangle_content.color =  listview.isCurrentItem ? "lightsteelblue" : "transparent"
                        console.debug("Meme")
                    }
                    else
                        console.debug("Pas meme")
                    console.debug("Nb = " + listview.currentIndex +" "+ number.text)
                }
            }

            Image {
                id: image
                state: page.state = "normal"
                anchors.left: parent.left
                fillMode: Image.PreserveAspectFit
                source: icon_img
                width: 70
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 0
                anchors.top: rectangle_title.bottom
                anchors.topMargin: 0
                states: [
                    State {
                        name: "delete"
                        PropertyChanges { target: image; source: "qrc:/delete"}
                    },
                    State {
                        name: "normal"
                        PropertyChanges { target: image;source: icon_img}
                    }
                ]
            }
            Text{
                id:number
                visible: false
                opacity: 0
                text: file_number
            }
            Rectangle{
                id: rectangle_title
                height: 30
                anchors.right: parent.right
                anchors.left: parent.left
                anchors.leftMargin: 0
                color: "gray"
                opacity: 0.6
                Text {
                    id: title
                    text: titre
                    anchors.top: parent.top
                    anchors.topMargin: 0
                    anchors.left: parent.left
                    anchors.bottom: parent.bottom
                    font.pixelSize: 23
                    font.bold: true
                }
            }
            Text {
                id: subTitle
                text: qsTr("Size:") + size +"Mb\n"+
                      qsTr("Modified") + modified +"\n"

                anchors.leftMargin: 0
                anchors.rightMargin: 0
                anchors.left: image.right
                anchors.top: rectangle_title.bottom
                anchors.right: button_go.left
                font.pixelSize: 20
            }
            Rectangle{
                id: button_go
                color: "transparent"
                width: 40
                anchors.bottom: parent.bottom
                anchors.bottomMargin: 0
                anchors.top: rectangle_title.bottom
                anchors.topMargin: 0
                anchors.right: parent.right
                anchors.rightMargin: 0
                Image {
                    id: text_button
                    source: file ? "qrc:/down" : "qrc:/next"
                    sourceSize.height: 20
                        sourceSize.width: 20
//                    text: file ? "|" : ">"
//                                      color: ListView.isCurrentItem ? "black" : "red"
//                    font.pointSize: 10
//                    font.bold: false
                    anchors.verticalCenter: parent.verticalCenter
                }
//                Text {
//                    id: text_button
//                    text: file ? "|" : ">"
////                                      color: ListView.isCurrentItem ? "black" : "red"
//                    font.pointSize: 10
//                    font.bold: false
//                    anchors.verticalCenter: parent.verticalCenter
//                }

                MouseArea{
                    id:go_mouse
                    anchors.fill: parent
                    onClicked: {
                        console.debug(title.text);                        
                        if(file)
                        {
                            ftpManager.downloadFile(title.text);
                            console.debug("Type = File");
                            Script.time_debut = Qt.formatDateTime(new Date(), "hhmmss");

                        }
                        else
                        {
                            ftpManager.Cd(title.text);
                            console.debug("[QML] ------ Directory --------");
                            console.debug("Go to " + title.text)
                            Script.addArray(title.text)
//                            Script.directoryName. = title.text
                            console.debug("[QML] ------ ------- --------");
                            loading();
                        }
                    }
                }
            }

        }
    }

    ProgressBar{
        id: progressBar
        x: 0
        y: 40
        height: 10
        opacity: 1
        visible: true
        anchors.top: pageHeader.bottom
        anchors.topMargin: 0
        anchors.left: parent.left
        anchors.leftMargin: 0
        anchors.right: parent.right
        anchors.rightMargin: 0
        indeterminate: false
        Behavior on opacity { NumberAnimation { duration: 50 }}

        }
    states: [
        State {
            name: "hideBrowser"
            PropertyChanges {
                target: localBrowser
                height: 0
            }
            PropertyChanges {
                target: listview
            }
        },
        State {
            name: "showBrowser"

            PropertyChanges {
                target: localBrowser
                height: 300
            }
        }
    ]
    transitions: [
        Transition {
            from: "hideBrowser"
            to: "showBrowser"
            PropertyAnimation {
                target: localBrowser;
                properties: "height"
                duration: 600
            }

        },
        Transition {
            from: "showBrowser"
            to: "hideBrowser"
            PropertyAnimation {
                target: localBrowser;
                properties: "height"
                duration: 600
            }
        }

    ]
}
